package impl.repository;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import dao.AccessDatabase;
import domain.CartItem;
import domain.Product;
import domain.ProductApparel;
import interfaces.repository.ProductApparelRepository;

public class StubProductApparelRepositoryImpl implements
		ProductApparelRepository {

	@Override
	public ProductApparel findProductByID(Integer id) {

		ProductApparel product;

		try {

			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			ResultSet rs = st
					.executeQuery("SELECT * FROM productapparel where idProductApparel='"
							+ id + "';");

			product = new ProductApparel(

			rs.getInt("idProductApparel"), rs.getString("name"),
					rs.getString("description"), rs.getString("picture"),
					rs.getFloat("price")

			) {
			};

			product.setColor(rs.getString("color"));
			product.setSize(rs.getFloat("size"));
			product.setMaterial(rs.getString("material"));
			con.close();

			return product;

		} catch (SQLException ex) {
			Logger.getLogger(ProductApparelRepository.class.getName()).log(
					Level.SEVERE, null, ex);

			return null;
		}

	}

	@Override
	public ProductApparel findProductByName(String name) {

		ProductApparel product;

		try {

			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			ResultSet rs = st
					.executeQuery("SELECT * FROM productapparel where name='"
							+ name + "';");

			product = new ProductApparel(

			rs.getInt("idProductApparel"), rs.getString("name"),
					rs.getString("description"), rs.getString("picture"),
					rs.getFloat("price")

			) {
			};

			product.setColor(rs.getString("color"));
			product.setSize(rs.getFloat("size"));
			product.setMaterial(rs.getString("material"));
			con.close();

			return product;

		} catch (SQLException ex) {
			Logger.getLogger(ProductApparelRepository.class.getName()).log(
					Level.SEVERE, null, ex);

			return null;
		}

	}

	@Override
	public boolean add(ProductApparel product) {
		try {
			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			st.execute("INSERT INTO `productapparel` (`name`,`picture`,`price`,`description`,`size`,`color`,`material`) VALUES ('"
					+ product.getName()
					+ "','"
					+ product.getPicture()
					+ "','"
					+ product.getPrice()
					+ "','"
					+ product.getDescription()
					+ "','"
					+ product.getSize()
					+ "','"
					+ product.getColor()
					+ "','" + product.getMaterial() + "');");
			con.close();

			return true;
		} catch (Exception t) {

			return false;

		}
	}

	@Override
	public boolean edit(ProductApparel before, ProductApparel after) {

		try {

			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			st.execute("UPDATE `productapparel` SET `name`='" + after.getName()
					+ "', `picture`='" + after.getPicture() + "', `price`='"
					+ after.getPrice() + "', `description`='"
					+ after.getDescription() + "', `size`='" + after.getSize()
					+ "', `color`='" + after.getColor() + "', `material`='"
					+ after.getMaterial() + "'  WHERE `idProductApparel`='"
					+ before.getId() + "'");
			con.close();

			return true;
		} catch (Exception t) {

			return false;

		}

	}

	@Override
	public boolean delete(Integer id) {

		try {
			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			st.execute("DELETE FROM `productapparel` WHERE `idProductApparel`='"
					+ id + "'");
			con.close();

			return true;
		} catch (Exception t) {

			return false;

		}
	}

	@Override
	public List<ProductApparel> findAllProduct() {
		ArrayList<ProductApparel> products = new ArrayList<ProductApparel>();
		ProductApparel product;

		try {

			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("SELECT * FROM productapparel");

			while (rs.next()) {

				product = new ProductApparel(

				rs.getInt("idProductApparel"), rs.getString("name"),
						rs.getString("description"), rs.getString("picture"),
						rs.getFloat("price")

				) {
				};

				product.setColor(rs.getString("color"));
				product.setSize(rs.getFloat("size"));
				product.setMaterial(rs.getString("material"));
				con.close();
				products.add(product);

			}
			con.close();
			return products;
		} catch (SQLException ex) {
			return null;
		}
	}

}
